package com.bazaaroid.server.gae.api.resources.partner;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.bazaaroid.server.gae.core.dao.partner.ProductDAO;
import com.bazaaroid.server.gae.core.dto.partner.AllEntitiesDTO;
import com.bazaaroid.server.gae.core.dto.partner.ProductDTO;
import com.bazaaroid.server.gae.core.dto.partner.AllEntitiesDTO.ProductsDTO;
import com.bazaaroid.server.gae.core.ioc.GuiceFactory;
import com.bazaaroid.server.gae.core.persistence.model.partner.Partner;
import com.bazaaroid.server.gae.core.persistence.model.partner.Product;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@Path("/product/")
public class ProductResource {
	@GET
	@Produces( { "application/xml", "application/json" })
	@Path("/{partnerId}/{productId}")
	public ProductDTO getById(
			@PathParam ("partnerId") Long partnerId,
			@PathParam ("productId") Long productId) {
		Key partnerKey = KeyFactory.createKey(
				Partner.class.getSimpleName(), partnerId);
		Key productKey = KeyFactory.createKey(
				partnerKey, Product.class.getSimpleName(), productId);
		ProductDAO productDAO = GuiceFactory.getInjector().getInstance(ProductDAO.class);
		Product product = productDAO.getById(productKey);
		return new ProductDTO(product);
	}

	@GET
	@Produces( { "application/xml", "application/json" })
	@Path("/all/")
	public ProductsDTO getAllProducts() {
		ProductDAO productDAO = GuiceFactory.getInjector()
			.getInstance(ProductDAO.class);
		List<Product> products = productDAO.getAll();
		return new AllEntitiesDTO.ProductsDTO(products);
	}
}
